package org.springframework.util;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author tonyiscoding
 */
public final class ReaderHelper {

	private static final boolean ENABLE = false;
	private static final String PURPOSE_BEAN_NAME = "lifeCycleBean";

	public static void beanLifecycleTrace(String format, String beanName){
		if(!ENABLE || !PURPOSE_BEAN_NAME.equals(beanName)){
			return;
		}
		final String msg = String.format(format, beanName);
		StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
		StackTraceElement callStack = stackTrace[2];
		String method = callStack.getClassName() + "." + callStack.getMethodName();
		method = method.substring(20);
		String outMsg = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")) +
				" [" + String.format("%-25s", Thread.currentThread().getName()) + "] " + method;
		if (msg != null && !"".equals(msg)) {
			outMsg = outMsg + " =>  " + msg;
		}
		System.out.println(outMsg);
	}

	public static void tip(String format, Object... args) {
		if (!ENABLE) {
			return;
		}
		final String msg = String.format(format, args);
		StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
		StackTraceElement callStack = stackTrace[2];
		String method = callStack.getClassName() + "." + callStack.getMethodName();
		method = method.substring(20);
		String outMsg = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")) +
				" [" + String.format("%-25s", Thread.currentThread().getName()) + "] " + method;
		if (msg != null && !"".equals(msg)) {
			outMsg = outMsg + " =>  " + msg;
		}
		System.out.println(outMsg);

	}

	public static void tip(String msg) {
		if (!ENABLE) {
			return;
		}
		StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
		StackTraceElement callStack = stackTrace[2];
		String method = callStack.getClassName() + "." + callStack.getMethodName();
		method = method.substring(20);
		String outMsg = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")) +
				" [" + String.format("%-25s", Thread.currentThread().getName()) + "] " + method;
		if (!"".equals(msg)) {
			outMsg = outMsg + " =>  " + msg;
		}
		System.out.println(outMsg);
	}

	public static void formatTrace() {
//		if (!ENABLE) {
//			return;
//		}
		StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
		StackTraceElement callStack = stackTrace[2];
		List<StackTraceElement> printTraces = Arrays
				.stream(stackTrace)
				.filter(e -> (e.toString().startsWith("org.springframework") || e.toString().startsWith("org.cnc")))
				.collect(Collectors.toList());
		if (printTraces.size() > 2) {
			printTraces = printTraces.subList(1, Math.min(10, printTraces.size()));
		}

		System.out.println("打印调用追踪: " + callStack.getClassName() + "." + callStack.getMethodName());
		String space = "|__";
		for (StackTraceElement traceElement : printTraces) {
			System.out.println(space + traceElement.toString());
			space = "  " + space;
		}
	}



}
